package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayout.Tab;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.android.material.tabs.TabLayoutMediator.TabConfigurationStrategy;
import easier.recycler.BaseViewHolder;
import easier.recycler.MultiTypeAdapter;
import easier.recycler.TypeViewHolder;
import java.util.ArrayList;
import java.util.List;

/**
 * 参考https://blog.csdn.net/yechaoa/article/details/122270969
 */
public class MainActivity10 extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager2 mViewPager;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity10.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main10 );
        initView();
    }

    private void initView() {

        mTabLayout = (TabLayout) findViewById( R.id.tabLayout );
        mViewPager = (ViewPager2) findViewById( R.id.viewPager );
        mTabLayout.setTabMode( TabLayout.MODE_AUTO );
        //for ( int i = 0; i < 5; i++ ) {
        //    Tab tab = mTabLayout.newTab();
        //    View view = View.inflate( mViewPager.getContext(), R.layout.tablayout_item, null );
        //    ((TextView) view.findViewById( R.id.text )).setText( "item:" + i );
        //    tab.setCustomView( view );
        //    mTabLayout.addTab( tab );
        //}

        mViewPager.setAdapter( new MultiTypeAdapter( convert() ) );

        new TabLayoutMediator( mTabLayout, mViewPager, new TabConfigurationStrategy() {
            @Override
            public void onConfigureTab( @NonNull Tab tab, int position ) {

                View view = View.inflate( mViewPager.getContext(), R.layout.tablayout_item, null );
                TextView textView = (TextView) view.findViewById( R.id.text );
                textView.setText( "item:" + position );
                tab.setCustomView( view );
            }
        } ).attach();
    }

    private List< TypeViewHolder > convert() {

        List< TypeViewHolder > list = new ArrayList<>();
        for ( int i = 0; i < 5; i++ ) {
            list.add( build() );
        }
        return list;
    }

    private TypeViewHolder build() {

        return new BaseViewHolder( R.layout.view_pager_item_00 );
    }
}